﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.XSSF.UserModel;
using Warehouse.Application.Dto.Input;
using Warehouse.Application.Dto.Output;
using Warehouse.Application.Services.SuppliedMaterials;
using Warehouse.Result;

namespace Warehouse.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SuppliedMaterialsController : ControllerBase
    {
        private readonly ISuppliedMaterialsAppService _suppliedMaterialsAppService;

        public SuppliedMaterialsController(ISuppliedMaterialsAppService suppliedMaterialsAppService)
        {
            _suppliedMaterialsAppService = suppliedMaterialsAppService;
        }

        /// <summary>
        /// 来料标签打印
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]

        public ApiResult<int> CreateSuppliedMaterials(CreateSuppliedMaterialsInput input)
        {
            return _suppliedMaterialsAppService.CreateSuppliedMaterials(input);
        }


        /// <summary>
        /// 获取来料标签
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiPaing<ListSuppliedMaterialsOutput> GetSuppliedMaterials([FromQuery]PageInput input)
        {
            return _suppliedMaterialsAppService.GetSuppliedMaterials(input);
        }

        /// <summary>
        /// 来料打印导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Derive()
        {
            var book = new XSSFWorkbook();
            var a = book.CreateSheet("标题");
            var row = a.CreateRow(0);
            row.CreateCell(0).SetCellValue("来料打印编号");
            row.CreateCell(1).SetCellValue("来源");
            row.CreateCell(2).SetCellValue("送货单号");
            row.CreateCell(3).SetCellValue("RID");
            row.CreateCell(4).SetCellValue("数量");
            row.CreateCell(5).SetCellValue("物料编码");
            row.CreateCell(6).SetCellValue("物料名称");

            var index = 1;
            var bs =  _suppliedMaterialsAppService.GetSuppliedMaterials(new PageInput { PageIndex = 1, PageSize = 1000 });
            foreach (var s in bs.PageDatas)
            {
                var rows = a.CreateRow(index++);
                rows.CreateCell(0).SetCellValue(s.Id);
                rows.CreateCell(1).SetCellValue(s.Source);
                rows.CreateCell(2).SetCellValue(s.DeliveryNumber);
                rows.CreateCell(3).SetCellValue(s.Rid);
                rows.CreateCell(4).SetCellValue(s.PrintNumber.ToString());
                rows.CreateCell(5).SetCellValue(s.MaterialId);
                rows.CreateCell(6).SetCellValue(s.MaterialName);
            }

            byte[] sb = null;
            using (var ms = new MemoryStream())
            {
                book.Write(ms);
                sb = ms.ToArray();
            }
            return File(sb, System.Net.Mime.MediaTypeNames.Application.Octet, "来料信息打印.xlsx");

        }






    }
}
